import java.io.*;
import java.net.*;
import java.util.*;

import javax.swing.*;

public class Server implements Connector, Runnable {

	private ServerSocket server = null;
	private Socket client = null;
	private Game game;
	private PrintWriter out = null;
	private Scanner in = null;

	public Server(Game game) {
		this.game = game;
		try {
			server = new ServerSocket(5291);
			client = server.accept();
			out = new PrintWriter(client.getOutputStream(),true);
			in = new Scanner(client.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null,"Es konnte keine Verbindung  hergestellt werden.",
							"Verbindungsproblem", JOptionPane.ERROR_MESSAGE);
		}
		new Thread(this).start();
	}

	@Override
	public void sendAction(int i) {
		out.print(i);
	}

	@Override
	public void run() {
		int i = in.nextInt(-1);
		if (i != -1){
			game.dropStone(i);
		}
		game.getGf().getMousePanel().setListenerBlocked(false);
	}

}
